<?php

namespace plugins\yongtai\controller;


use plugins\yongtai\controller\Base\AdminBaseController;
use plugins\yongtai\model\ArticleCategoryModel;
use plugins\yongtai\model\ArticleModel;

class ArticleController extends AdminBaseController
{
    function Index()
    {
        $edit_url = cmf_plugin_url("yongtai://Article/edit_article");
        $del_url = cmf_plugin_url("yongtai://Article/del_article");
        $table_config = [
            ['field' => 'id', "content" => 'ID', "width" => "60", "align" => "center"],
            ['field' => 'listorder', "content" => '排序', "width" => "60", "align" => "center"],
            ["type" => "image", 'field' => 'thumb', "content" => '缩略图', "width" => "70"],
            ['field' => 'title', "content" => '标题', "width" => "420"],
            ['field' => 'description', "content" => '简介'],
            ['type' => 'toolbar', "content" => '操作', "width" => "220",
                "buttons" => [
                    ['content' => '修改', "callable" => "showDialog", 'params' => "{url:'{$edit_url}'}"],
                    ['content' => '删除', "callable" => "showConfirm", 'params' => "{url:'{$del_url}',title:'是否删除？'}"],
                ]],
        ];
        $this->assign('table_config', $table_config);
        return $this->Render(__CLASS__, __FUNCTION__);
    }

    function GetListPage()
    {
        $page = $this->GetPage();
        $data = (new ArticleModel())->GetListPage([], $page['page'], $page['page_size']);
        $table_data = $data;
        return json($table_data);
    }

    function add_article()
    {
        $role = [
            'title' => "require",
        ];
        $this->AddPost((new ArticleModel()), $role, function ($data) {
            $data['user_id'] = cmf_get_current_admin_id();
            $data['create_time'] = time();
            return $data;
        });

        $form = [
            ['field' => 'listorder', 'type' => "number", 'content' => '排序', 'value' => "0", "placeholder" => "排序"],
            ['field' => 'title', 'type' => "text", 'content' => '标题', 'value' => "", "placeholder" => "标题"],
            ['field' => 'pid', 'type' => "select", 'content' => '上级分类', 'value' => 0, "placeholder" => "最上层",
                "options" => (new ArticleCategoryModel())->CategoryOptions()
            ],
            ['field' => 'thumb', 'type' => "upload", 'content' => '缩略图', 'value' => "", "number" => 1],
            ['field' => 'description', 'type' => "textarea", 'content' => '简介', 'value' => "", "placeholder" => "简介"],
            ['field' => 'content', 'type' => "richtext", 'content' => '内容', 'value' => "", "selector" => "selector"],
            ['field' => 'display', 'type' => "radio", 'content' => '显示', "value" => '1',
                "options" => [
                    ['content' => "显示", 'value' => 1],
                    ['content' => "隐藏", 'value' => 0],
                ]
            ],
        ];
        $this->GenerateForm($form, __CLASS__, __FUNCTION__);
        return $this->fetch("admin/public/form");
    }

    function edit_article()
    {
        $data = $this->GetEditData((new ArticleModel()));
        $role = [
            'title' => "require",
        ];
        $this->EditPost((new ArticleModel()), $data['id'], $role);

        $form = [
            ['field' => 'listorder', 'type' => "number", 'content' => '排序', 'value' => $data['listorder'], "placeholder" => "排序"],
            ['field' => 'title', 'type' => "text", 'content' => '标题', 'value' => $data['title'], "placeholder" => "标题"],
            ['field' => 'pid', 'type' => "select", 'content' => '上级分类', 'value' => $data['pid'], "placeholder" => "最上层",
                "options" => (new ArticleCategoryModel())->CategoryOptions()
            ],
            ['field' => 'thumb', 'type' => "upload", 'content' => '缩略图', 'value' => $data['_thumb'], "number" => 1],
            ['field' => 'description', 'type' => "textarea", 'content' => '简介', 'value' => $data['description'], "placeholder" => "简介"],
            ['field' => 'content', 'type' => "richtext", 'content' => '内容', 'value' => $data['content'], "selector" => "selector"],
            ['field' => 'display', 'type' => "radio", 'content' => '显示', "value" => (string)$data['display'],
                "options" => [
                    ['content' => "显示", 'value' => 1],
                    ['content' => "隐藏", 'value' => 0],
                ]
            ],
        ];
        $this->GenerateForm($form, __CLASS__, __FUNCTION__);
        return $this->fetch("admin/public/form");
    }

    function del_article()
    {
        $this->DelPost((new ArticleModel()));
    }
}
